<?php
/*
    zygote.class
*/
include_once('genome.class.php');
class zygote {
    private $male;
    private $female;
   
/* zygote */ 
    public function new_zygote (genome $mg,genome $fg) {
	$this->male = new genome ();
	$this->female = new genome ();
	$this->male->chromosome = $mg->chromosome;
	$this->female->chromosome = $fg->chromosome;
	
    }
/* getMale  */
    public function get_male () {
	return $this->male;
    }
/* getFemale */
    public function get_female () {
	return $this->female;
    }
/* getChild  */
    public function get_child (random $r) {
	$p = $r->next_int();
	if ( $p < 0 ) {
	    return $this->female;
	} else {
	    return $this->male;
	} 
    }
/* crossing_over */
    public function crossing_over (random $r,$nCross) {
	$lenGenome = genome::$NBGENE;
	
	for ( $k = 0; $k < $nCross; $k++ ) {
	    $p = $r->next_int() % $lenGenome;
	    $p = $p < 0 ? -$p : $p;
	    for ( $i = $p; $i < $lenGenome; $i++ ) {
		$temp = $this->male->chromosome[$i];
		$this->male->chromosome[$i] = $this->female->chromosome[$i];
		$this->female->chromosome[$i] = $temp;
	    }
	}
    }
}

// test
/*
$t = new zygote();
$r = new random();
$g1 = new genome();
$g2 = new genome();
$g1->new_ran_genome($r);
$g2->new_ran_genome($r);

$t->new_zygote($g1,$g2);
$t->crossing_over($r,2);
*/
?>
